home *** CD-ROM | disk | FTP | other *** search
- /*----------------------------------------------------------------------
- * $Id: gettime.c,v 1.8 1993/01/22 10:52:55 carlson Exp $
- *
- * gettime.c Gets the last modification time of the file and
- * writes it to stdout.
- *
- * Synopsis:
- * gettime [-d] [-i] [-c | -m | -a] [[+<format>] <file>] [[+<format>] \
- * <file>] ...
- *
- * Description:
- * This routine writes to stdout the file time for the file(s)
- * specified. If -c is specified, the creation time is written;
- * -m for modification time and -a for access time. Modification
- * time is assumed. If format is specified, it will display the
- * time for all the following files (until another format is
- * specified) in the format given. Otherwise, the date and time
- * will be printed according to the default of ctime(3C). Format
- * is defined the same way as for the date(1) command.
- *
- * The -d option enables debug printout.
- *
- * The -i option overrides any format(s) provided and prints
- * the date in integer format.
- *
- * Revision history:
- * $Log: gettime.c,v $
- * Revision 1.8 1993/01/22 10:52:55 carlson
- * Just changed the header line to use RCS Id, instead of Header.
- *
- * Revision 1.7 91/11/22 15:02:53 carlson
- * Get time printed properly, instead of the address.
- *
- * Revision 1.6 91/07/28 00:36:26 carlson
- * Allow a list of files to be processed.
- *
- * Revision 1.5 91/07/22 13:33:05 carlson
- * Modified to allow multiple files on the command line.
- *
- * Revision 1.4 91/07/19 14:18:18 carlson
- * Added Header for RCS.
- *
- * Revision 1.3 91/07/19 13:51:15 carlson
- * Added -i option which prints in the time in decimal.
- *
- * Revision 1.2 91/07/19 13:25:20 carlson
- * Modified to use new format for cftime which requires % symbols.
- *
- * Revision 1.1 90/10/09 17:24:45 chris
- * Initial revision
- *
- * 12 Sep 1989 Christopher W. Carlson, Silicon Graphics Inc.
- * Initial draft
- *
- *--------------------------------------------------------------------*/
-
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <errno.h>
- #include <time.h>
- #include <ctype.h>
-
- static struct stat f_stat;
- static struct tm tim_st;
- static char cbuff[256], format[256] = "";
- static long timdat;
- static enum { CREATE, MODIFY, ACCESS } timetype = MODIFY;
- static enum { FALSE, TRUE } debug, intfmt, one;
-
- main (argc, argv)
- int argc;
- char *argv[];
- {
- register int i, j;
- register char *cpos;
-
- /*----
- * Initialize some variables.
- *----*/
-
- debug = FALSE; /* Assume no debug */
- intfmt = FALSE; /* Assume not integer format */
- one = TRUE; /* Assume only one file to do */
-
- /*----
- * Pick up the parameters.
- *----*/
-
- for (j = 1; j < argc; j++) {
-
- /*----
- * If the parameter starts with a '-', it is an option. If not,
- * it is the start of the +<format> <file> arguments.
- *----*/
-
- if (*argv[j] == '-') {
- switch (argv[j][1]) {
- case 'a':
- timetype = ACCESS;
- break;
-
- case 'c':
- timetype = CREATE;
- break;
-
- case 'd':
- debug = TRUE;
- break;
-
- case 'i':
- intfmt = TRUE;
- break;
-
- case 'm':
- timetype = MODIFY;
- break;
-
- default:
- fprintf (stderr, "gettime: Unrecognized option %s\n", argv[j]);
- break;
- }
- } else {
- break;
- }
- }
-
- /*----
- * Now we are looping through the +<format> <file> arguments.
- *----*/
-
- for (; j < argc; j++) {
-
- /*----
- * If the parameter starts with '+', it is a format.
- *----*/
-
- if (*argv[j] == '+') {
- for (i = 0, cpos = &argv[j][1]; *cpos; cpos++) {
- format[i++] = '%';
- format[i++] = *cpos;
- }
- format[i] = '\0';
-
- /*----
- * Otherwise, the parameter is a file name. Handle the file.
- *----*/
-
- } else {
-
- /*----
- * Let's find out if more than one file is to be processed.
- * If 'one' is TRUE, we currently believe we only have to
- * process one file. Let's check the next argument and if
- * it exists, we can assume we are going to process more than
- * one file and we can set 'one' to false. Note, if the next
- * argument begins with a '+' or '-', keep scanning down the
- * arguments until we find a legitimate argument.
- *----*/
-
- if (one) {
- for (i = j + 1; i < argc; i++) {
- if ((argv[i][0] != '+') && (argv[i][0] != '-')) {
- one = FALSE;
- break;
- }
- }
- }
-
- /*----
- * If we are processing more than one file, print the
- * file name in front of the time.
- *----*/
-
- if (! one)
- printf ("%s: ", argv[j]);
-
- /*----
- * Get the status of the file.
- *----*/
-
- if (stat (argv[j], &f_stat) < 0) {
- printf ("File status error %d\n", errno);
- continue;
- }
-
- /*----
- * Depending on which time the user wants, put it into timdat.
- *----*/
-
- switch (timetype) {
- case CREATE:
- timdat = f_stat.st_ctime;
- break;
-
- case MODIFY:
- timdat = f_stat.st_mtime;
- break;
-
- case ACCESS:
- timdat = f_stat.st_atime;
- break;
-
- default:
- fprintf (stderr, "gettime: *** System error 1 ***\n");
- exit (-1);
- }
-
- /*----
- * Print the time out. If -i option provided, print it in decimal.
- * If a format was provided, print using the format. Otherwise, just
- * print it in ctime format.
- *----*/
-
- if (intfmt) {
- printf ("%d\n", timdat);
- } else if (format[0] == '\0') {
- cpos = ctime (&timdat);
- printf ("%s", cpos);
- } else {
- cftime (cbuff, format, &timdat);
- printf ("%s\n", cbuff);
- }
- }
- }
- }
-